package com.yimi.yinhepda.views.unload_module;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pda.ym.base.BaseActivity;
import com.pda.ym.utils.FastClick;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.UnloadScanTableDao;
import com.yimi.yinhepda.db.dao.UnloadTaskTableDao;
import com.yimi.yinhepda.db.table.UnloadTaskTable;
import com.yimi.yinhepda.entity.bean.TaskEntity;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.views.unload_module.UnBlocking.BlockingUnloadTaskActivity;
import com.yimi.yinhepda.views.unload_module.UnBlocking.UnBlockingActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 到达卸车界面
 *
 * @author 000432
 */
public class UnloadActivity extends BaseActivity {
    private static final int COUNT = 1;
    private TextView num;
    private UnloadScanTableDao mUnloadScanTableDao;
    /**
     * 暂存任务提醒标记
     */
    private Button tvTempTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unload);
        initView();
        initData();
    }

    private void initData() {
        mUnloadScanTableDao = new UnloadScanTableDao();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置未上传数
        num.setText((int) mUnloadScanTableDao.queryCount("isUpload", 0, 2) + "");
        //查询暂存的配置单号
        List<TaskEntity> listTaskEntity = new ArrayList<>();
        List<UnloadTaskTable> listUnloadTaskTable = new UnloadTaskTableDao().findUnComplete(0, sharedPreferUtil.getValue(Common.USER_CODE));
        if (listUnloadTaskTable != null && listUnloadTaskTable.size() > 0) {
            for (UnloadTaskTable item : listUnloadTaskTable) {
                if (item.getOfflineTaskNo() != null) {
                    if (item.getOfflineTaskNo().startsWith("Z") || item.getOfflineTaskNo().startsWith("X")) {
                        continue;
                    }
                }
                boolean isExist = false;
                for (TaskEntity temp : listTaskEntity) {
                    if (temp.getTask() != null && item.getTask() != null) {
                        if (temp.getTask().equals(item.getTask())) {
                            isExist = true;
                        }
                    }
                }
                if (!isExist) {
                    listTaskEntity.add(new TaskEntity(item.getTask(), item.getStowageNo(), item.getCarMark(), item.getToCompCode(), item.getSrcDeptCode(), item.getSrcDeptName(),
                            item.getDestDeptCode(), item.getDestDeptName(), item.getCarNo(), item.getPriceNum(), false));
                }
            }
            tvTempTag.setText(listTaskEntity.size() + "");
            tvTempTag.setVisibility(View.VISIBLE);
            if (listTaskEntity.size() <= 0) {
                tvTempTag.setVisibility(View.GONE);
            }
        } else {
            tvTempTag.setVisibility(View.GONE);
        }
    }

    @Override
    public String getPageName() {
        return "到达卸车";
    }

    private void initView() {
        ((TextView) findViewById(R.id.title)).setText("到达卸车");
        num = (TextView) findViewById(R.id.num);
        findViewById(R.id.unload_scan_btn).setOnClickListener(this);
        findViewById(R.id.task_cache_btn).setOnClickListener(this);
        findViewById(R.id.unload_task_back_btn).setOnClickListener(this);
        findViewById(R.id.unload_blocking_btn).setOnClickListener(this);
        findViewById(R.id.unload_blocking_unload).setOnClickListener(this);
        tvTempTag = (Button) findViewById(R.id.unload_temp_save_tag);
    }

    @Override
    public void onClick(View v) {
        if (FastClick.isFastClick()) {
            return;
        }

        switch (v.getId()) {
            case R.id.unload_scan_btn:
                //卸车扫描
                if (sharedPreferUtil.getOfflineMode()) {
                    showToast(getString(R.string.no_use_in_offline));
                    return;
                }
                Intent intent = new Intent(UnloadActivity.this, CreateUnloadTaskActivity.class);
                startActivity(intent);
                break;
            case R.id.task_cache_btn:
                //任务暂存集
                Intent intent1 = new Intent(UnloadActivity.this, TaskCashCollectionActivity.class);
                startActivity(intent1);
                break;
            case R.id.unload_task_back_btn:
                finish();
                break;
            case R.id.unload_blocking_btn:
                //解封任务
                startActivity(new Intent(UnloadActivity.this, UnBlockingActivity.class));
                break;
            case R.id.unload_blocking_unload:
                //解封卸车
                startActivity(new Intent(UnloadActivity.this, BlockingUnloadTaskActivity.class));
                break;
            default:
                break;
        }
    }
}
